﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using DataCenter.Clr;
using DataCenter.Share;
using Microsoft.SqlServer.Server;
using System.Diagnostics;

public partial class UserDefinedFunctions
{
    [SqlFunction(TableDefinition = "ObjectId INT, ValueType INT, ValueTime DATETIME, OperType INT, R__0 REAL",
                 Name = "SelectValue",
                 FillRowMethodName = "FillValueRow",
                 SystemDataAccess = SystemDataAccessKind.Read,
                 DataAccess = DataAccessKind.Read)]
    public static IEnumerable SelectValue(SqlString ObjectId, SqlString ValueType, SqlString OperType, SqlDateTime StartTime, SqlDateTime EndTime, SqlInt32 Step, SqlInt32 ImportValue)
    {
        CenterEntry entry = new CenterEntry(1);

        if (!ImportValue.IsNull && ImportValue.Value == 1)
        {
            entry.CallApp(ExecTypes.Import | ExecTypes.Process | ExecTypes.Calculate,
                          ObjectId.IsNull ? "" : ObjectId.Value,
                          StartTime.IsNull ? Utils.EMPTY_TIME : StartTime.Value,
                          EndTime.IsNull ? DateTime.MaxValue : EndTime.Value);
        }

        return entry.Select(ObjectId.IsNull ? "" : ObjectId.Value,
                            ValueType.IsNull ? "" : ValueType.Value,
                            OperType.IsNull ? "" : OperType.Value,
                            StartTime.IsNull ? Utils.EMPTY_TIME : StartTime.Value,
                            EndTime.IsNull ? DateTime.MaxValue : EndTime.Value,
                            Step.IsNull ? 1 : (Step.Value <= 0 ? 1 : Step.Value));
    }
    public static void FillValueRow(object obj, out int objectId, out int valueType, out DateTime valueTime, out int operType, out SqlSingle value0)
    {
        ResultRecordRow row = (ResultRecordRow)obj;
        objectId = row.ObjectId;
        valueType = row.ValueType;
        valueTime = row.ValueTime;
        operType = row.OperType;        
        value0 = row.Values[0] ?? SqlSingle.Null;
    }

    [SqlFunction(TableDefinition = "ObjectId INT, ObjectType INT, ObjectName NVARCHAR(255)",
                 Name = "SelectObjectInfo",
                 FillRowMethodName = "FillInfoRow",
                 SystemDataAccess = SystemDataAccessKind.Read,
                 DataAccess = DataAccessKind.Read)]
    public static IEnumerable SelectObjectInfo(SqlString ObjectId, SqlString ObjectType)
    {
        CenterEntry entry = new CenterEntry(1);
        return entry.SelectInfo(ObjectId.IsNull ? "" : ObjectId.Value,
                                      ObjectType.IsNull ? "" : ObjectType.Value);         
    }
    public static void FillInfoRow(object obj, out int objectId, out int objectType, out string objectName)
    {
        DataRow row = (DataRow)obj;
        objectId = Convert.ToInt32(row["ObjectId"]);
        objectType = Convert.ToInt32(row["ObjectType"]);
        objectName = Convert.ToString(row["ObjectName"]);
    }

    [SqlFunction(TableDefinition = "ObjectId INT, ValueType INT, ValueTime DATETIME, R__0 REAL",
                 Name = "SelectRecentValue",
                 FillRowMethodName = "FillRecentRow",
                 SystemDataAccess = SystemDataAccessKind.Read,
                 DataAccess = DataAccessKind.Read)]
    public static IEnumerable SelectRecentValue(SqlString ObjectId, SqlString ValueType)
    {
        CenterEntry entry = new CenterEntry(1);
        return entry.SelectRecentValue(ObjectId.IsNull ? "" : ObjectId.Value,
                                       ValueType.IsNull ? "" : ValueType.Value);
    }
    public static void FillRecentRow(object obj, out int objectId, out int valueType, out DateTime valueTime, out float value)
    {
        DataRow row = (DataRow)obj;
        objectId = Convert.ToInt32(row["ObjectId"]);
        valueType = Convert.ToInt32(row["ValueType"]);
        valueTime = Convert.ToDateTime(row["ValueTime"]);
        value = Convert.ToSingle(row["R__0"]); 
    }

    [SqlFunction]
    public static SqlString GetVersion()
    {
        return CenterEntry.VERSION.ToString();
    }
};

